package com.hy.ssm.controller;



import com.hy.ssm.bean.ProductInfo;
import com.hy.ssm.biz.IProductInfoBiz;
import com.hy.ssm.biz.impl.ProductInfoBizImpl;
import com.hy.ssm.util.ConstantInfo;
import com.hy.ssm.util.WaterMarkUtil;
import com.hy.ssm.vo.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.security.auth.login.Configuration;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/product")
public class GoodsInfoController {
    @Autowired
    private IProductInfoBiz productInfoBiz;
    /**
     * 处理富文本编辑器中的文件上传的
     *
     * @param
     * @param
     */
    @PostMapping("/upload")
    public Map<String,Object> upload(MultipartFile upload) throws Exception {
        String savepath = "";//保存图片的相对路径,这个路径是要存数据库的
        File dest  = null;//最终要存放的文件
        Map<String,Object> result = new HashMap<>();

        if (upload != null && upload.getSize() > 0){//说明确实有文件要上传
            savepath = ConstantInfo.uploadpath + "/" + System.currentTimeMillis() + "_" + upload.getOriginalFilename();
            dest = new File(ConstantInfo.basepath,savepath);
            upload.transferTo(dest);//传输到指定文件

            WaterMarkUtil.textWaterMark(dest);

            // 注意：富文本编辑器ckeditor图片上传要求的返回值是一个map对象，里面包含fileName：文件名称，url：文件在服务器中的地址，uploaded：回调函数
            result.put("fileName", upload.getOriginalFilename());
            result.put("url", "../../" + savepath);
            result.put("uploaded", "1");
        }
        return result;
    }

    /**
     * 处理富文本编辑器中的文件上传的
     *
     * @param
     * @param
     */
    @PostMapping("/add")
    public ResultVO add(ProductInfo pf,MultipartFile[] product_pics){
        String savepath = "";//保存图片的相对路径,这个路径是要存数据库的
        File dest  = null;//最终要存放的文件

        if (product_pics != null && product_pics.length > 0 && product_pics[0].getSize() > 0){
            //说明确实有文件要上传
            String PicsStr = "";
            try {
                for (MultipartFile pic : product_pics) {
                    savepath = ConstantInfo.uploadpath + "/" + System.currentTimeMillis() + "_" + pic.getOriginalFilename();
                    dest = new File(ConstantInfo.basepath,savepath);
                    pic.transferTo(dest);//传输到指定文件

                    if ("".equals(PicsStr)){
                        PicsStr = savepath;
                    }else {
                        PicsStr += ";" + savepath;
                    }
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            pf.setPics(PicsStr);
        }

        int result = productInfoBiz.add(pf);

        // 响应
        if (result > 0) {
            return new ResultVO(200, "成功!");
        }
            return new ResultVO(500, "失败!");
    }

    /**
     * 第一次查询
     *
     * @param
     * @param
     */
    @PostMapping("/findFirst")
    public ResultVO findFirst(@RequestParam Map<String,Object> map) throws IOException {
        Map<String,Object> result = productInfoBiz.findByFirst(map);
        if (result == null || result.isEmpty()) {
            return new ResultVO(600,"暂无数据!");
        }
        return new ResultVO(200,result);
    }

    /**
     * 分页
     * @param
     * @param
     */
    @PostMapping("/finds")
    public ResultVO finds(@RequestParam Map<String,Object> map) throws IOException {
        List<ProductInfo> list =  productInfoBiz.finds(map);
        if (list == null || list.isEmpty()) {
            return new ResultVO(600,"暂无数据!");
        }
        return new ResultVO(200,list);
    }
}
